import React from 'react';
import { Icon, message, Modal } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaNewScroll, WeaTree } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class MaterialLibLeft extends React.Component {
    state = { datas: [] };

    constructor(props) {
        super(props);
        this.onSelect = this.onSelect.bind(this);
        this.getDirs = this.getDirs.bind(this);
        this.onLoadData = this.onLoadData.bind(this);
        this.onAdd = this.onAdd.bind(this);
        this.onEdit = this.onEdit.bind(this);
        this.onDelete = this.onDelete.bind(this);
        this.onValidate = this.onValidate.bind(this);
    }

    componentWillMount() {
        this.getDirs();
    }

    render() {
        const { hasRight } = this.props;
        const { datas } = this.state;
        return (
            <div className="portal-p4e-mll">
                <div className="portal-p4e-mll-top">
                    <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@7g6yy8`} type="bars" />
                    <span style={{ marginLeft: '5px' }}>{getLabel(385079, '全部目录')}</span>
                </div>
                <div className="portal-p4e-mll-middle">
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@kvg8rd`} height="100%">
                        <WeaTree ecId={`${this && this.props && this.props.ecId || ''}_WeaTree@yx3rnw`}
                          datas={datas}
                          loadData={this.onLoadData}
                          onSelect={this.onSelect}
                          onAdd={this.onAdd}
                          onEdit={this.onEdit}
                            // editHandler={this.onEdit}
                            // onDelete={this.onDelete}
                          deleteHandler={this.onDelete}
                          validate={this.onValidate}
                        />
                    </WeaNewScroll>
                </div>
                {
                    hasRight ? (
                            <div className="portal-p4e-mll-bottom">
                                <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@ocyun3`} type="exclamation-circle-o" />
                                <span style={{ marginLeft: '5px' }}>{getLabel(387215, '目录名称只能包含字母和数字')}</span>
                            </div>
                        ) : ''
                }
            </div>
        );
    }

    onSelect(dirs) {
        this.props.setDir(dirs[0]);
    }

    getDirs() {
        WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', {}).then((result) => {
            this.setState({ datas: result.datas || [] });
        });
    }

    onLoadData(node) {
        return WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', { dir: node.dir });
    }

    onAdd(value, node) {
        return WeaTools.callApi('/api/portal/materialLib/addDir', 'POST', { dir: node.dir, dirname: value });
    }

    onEdit(value, node) {
        if (node.isDefaultFile) {
            message.error('系统默认目录不支持编辑！！');
        } else {
            return WeaTools.callApi('/api/portal/materialLib/renameDir', 'POST', { pdir: node.parentId, dir: node.dir, dirname: value });
        }
    }

    onDelete(item) {
        if (item.isDefaultFile) {
            message.error('系统默认目录不支持删除！！');
        } else {
            Modal.confirm({
                title: getLabel(19085, '消息提醒'),
                content: getLabel(501356, '确定要删除该目录下所有目录以及文件吗'),
                onOk: () => {
                    WeaTools.callApi('/api/portal/materialLib/deleteDir', 'POST', { dir: item.dir });
                    this.setState({
                        datas: [],
                    });
                    this.getDirs();
                },
            });
        }
    }

    onValidate(node) {
        if (!node.name.toLowerCase().match(/^[0-9A-Za-z]+$/)) {
            message.warning(getLabel(387215, '目录名称只能包含字母和数字'));
            return false;
        }
            return true;
    }
}

export default MaterialLibLeft;
